package Servlet.Student;

import Entity.MyTableData;
import Entity.Role.Teacher;
import Entity.TitleAndPaper.Paper;
import Entity.TitleAndPaper.Title;
import Service.Student.Imp.StudentTitleServiceImp;
import Service.Student.StudentTitleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/titleInfo", "/studentAddPaper", "/titleSelect", "/getTeacherInfo"})
public class StudentTitleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/titleInfo".equals(url)) {
            titleInfo(request, response);
        }  else if ("/studentAddPaper".equals(url)) {
            studentAddPaper(request, response);
        } else if ("/titleSelect".equals(url)) {
            titleSelect(request, response);
        } else if ("/getTeacherInfo".equals(url)) {
            getTeacherInfo(request, response);
        }
    }

    /**
     * 根据工号获取教师信息
     *
     */
    private void getTeacherInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String tno = request.getParameter("tno");
        StudentTitleService studentTitleService = new StudentTitleServiceImp();
        Teacher teacher = studentTitleService.getTeacherInfo(tno);
        String json = JSONObject.toJSONString(teacher, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();

    }

    /**
     * 学生选择课题后生成论文（任务书阶段）
     *
     */
    private void studentAddPaper(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentTitleService studentTitleService = new StudentTitleServiceImp();
        String NO = request.getParameter("userName");
        String data = request.getParameter("taskData");
        JSONObject json = JSON.parseObject(data);
        Paper paper = new Paper();
        paper.setSno(NO);
        paper.setState("任务书");
        paper.setTitle(json.get("title").toString());
        paper.setCid(json.get("collegeName").toString());
        paper.setTno(json.get("tno").toString());
        paper.setXtId(Integer.parseInt(json.get("xtId").toString()));
        boolean flag = studentTitleService.addStudentTitle(paper);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }



    /**
     * 根据课题名称模糊查询课题
     *
     */
    private void titleSelect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentTitleService studentTitleService = new StudentTitleServiceImp();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        String sno = request.getParameter("sno");
        String taskData = request.getParameter("taskData");
        String keyword = "%%";
        if (taskData != null) {
            JSONObject jsonObject = JSON.parseObject(taskData);
            keyword = "%" + jsonObject.getString("title") + "%";
        }
        MyTableData myTableData = studentTitleService.titleSelect(sno, keyword,offset,limit);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 显示课题表格信息
     *
     */
    private void titleInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sno = request.getParameter("sno");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        StudentTitleService studentTitleService = new StudentTitleServiceImp();
        MyTableData myTableData = studentTitleService.getTitleInfo(sno,offset,limit);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


}

